﻿namespace OboutInc.EasyMenu_Pro
{
    using System;
    using System.ComponentModel;
    using System.Reflection;
    using System.Web.UI;
    using System.Xml;

    [Serializable, ToolboxItem(false)]
    public class CSSClasses
    {
        private string _Component;
        private string _ComponentContentCell;
        private string _ComponentContentCellOver;
        private string _ComponentIconCell;
        private string _ComponentIconCellOver;
        private string _ComponentLeftRoundCornerCell;
        private string _ComponentLeftRoundCornerCellOver;
        private string _ComponentOver;
        private string _ComponentRightRoundCornerCell;
        private string _ComponentRightRoundCornerCellOver;
        private string _ComponentSubMenuCell;
        private string _ComponentSubMenuCellOver;
        private Type objectType;

        public CSSClasses() : this(typeof(MenuItem))
        {
            this.objectType = null;
        }

        public CSSClasses(Type menuComponentType)
        {
            this._Component = "";
            this._ComponentOver = "";
            this._ComponentContentCell = "";
            this._ComponentContentCellOver = "";
            this._ComponentLeftRoundCornerCell = "";
            this._ComponentLeftRoundCornerCellOver = "";
            this._ComponentRightRoundCornerCell = "";
            this._ComponentRightRoundCornerCellOver = "";
            this._ComponentIconCell = "";
            this._ComponentIconCellOver = "";
            this._ComponentSubMenuCell = "";
            this._ComponentSubMenuCellOver = "";
            if (!menuComponentType.IsSubclassOf(typeof(ItemBase)) && !menuComponentType.Equals(typeof(ItemBase)))
            {
                throw new MemberNotDerivedFromMenuComponent();
            }
            this.objectType = menuComponentType;
            while (true)
            {
                if ((menuComponentType.GetProperty("Component", typeof(string)) != null) || (menuComponentType.BaseType == null))
                {
                    break;
                }
                menuComponentType = menuComponentType.BaseType;
            }
            PropertyInfo[] properties = menuComponentType.GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
            for (int i = 0; i < properties.Length; i++)
            {
                if (properties[i].PropertyType == typeof(string))
                {
                    switch (properties[i].Name.ToLower())
                    {
                        case "component":
                            this.Component = properties[i].GetValue(null, null).ToString();
                            break;

                        case "componentover":
                            this.ComponentOver = properties[i].GetValue(null, null).ToString();
                            break;

                        case "componentcontentcell":
                            this.ComponentContentCell = properties[i].GetValue(null, null).ToString();
                            break;

                        case "componentcontentcellover":
                            this.ComponentContentCellOver = properties[i].GetValue(null, null).ToString();
                            break;

                        case "componentleftroundcornercell":
                            this.ComponentLeftRoundCornerCell = properties[i].GetValue(null, null).ToString();
                            break;

                        case "componentleftroundcornercellover":
                            this.ComponentLeftRoundCornerCellOver = properties[i].GetValue(null, null).ToString();
                            break;

                        case "componentrightroundcornercell":
                            this.ComponentRightRoundCornerCell = properties[i].GetValue(null, null).ToString();
                            break;

                        case "componentrightroundcornercellover":
                            this.ComponentRightRoundCornerCellOver = properties[i].GetValue(null, null).ToString();
                            break;

                        case "componenticoncell":
                            this.ComponentIconCell = properties[i].GetValue(null, null).ToString();
                            break;

                        case "componenticoncellover":
                            this.ComponentIconCellOver = properties[i].GetValue(null, null).ToString();
                            break;

                        case "componentsubmenucell":
                            this.ComponentSubMenuCell = properties[i].GetValue(null, null).ToString();
                            break;

                        case "componentsubmenucellover":
                            this.ComponentSubMenuCellOver = properties[i].GetValue(null, null).ToString();
                            break;
                    }
                }
            }
        }

        public CSSClasses(Type menuComponentType, string Component, string ComponentOver, string ComponentContentCell, string ComponentContentCellOver, string ComponentLeftRoundCornerCell, string ComponentLeftRoundCornerCellOver, string ComponentRightRoundCornerCell, string ComponentRightRoundCornerCellOver, string ComponentIconCell, string ComponentIconCellOver, string ComponentSubMenuCell, string ComponentSubMenuCellOver)
        {
            this._Component = "";
            this._ComponentOver = "";
            this._ComponentContentCell = "";
            this._ComponentContentCellOver = "";
            this._ComponentLeftRoundCornerCell = "";
            this._ComponentLeftRoundCornerCellOver = "";
            this._ComponentRightRoundCornerCell = "";
            this._ComponentRightRoundCornerCellOver = "";
            this._ComponentIconCell = "";
            this._ComponentIconCellOver = "";
            this._ComponentSubMenuCell = "";
            this._ComponentSubMenuCellOver = "";
            if (!menuComponentType.IsSubclassOf(typeof(ItemBase)) && !menuComponentType.Equals(typeof(ItemBase)))
            {
                throw new MemberNotDerivedFromMenuComponent();
            }
            this.objectType = menuComponentType;
            while (true)
            {
                if ((menuComponentType.GetProperty("Component", typeof(string)) != null) || (menuComponentType.BaseType == null))
                {
                    break;
                }
                menuComponentType = menuComponentType.BaseType;
            }
            this.Component = Component;
            this.ComponentOver = ComponentOver;
            this.ComponentContentCell = ComponentContentCell;
            this.ComponentContentCellOver = ComponentContentCellOver;
            this.ComponentLeftRoundCornerCell = ComponentLeftRoundCornerCell;
            this.ComponentLeftRoundCornerCellOver = ComponentLeftRoundCornerCellOver;
            this.ComponentRightRoundCornerCell = ComponentRightRoundCornerCell;
            this.ComponentRightRoundCornerCellOver = ComponentRightRoundCornerCellOver;
            this.ComponentIconCell = ComponentIconCell;
            this.ComponentIconCellOver = ComponentIconCellOver;
            this.ComponentSubMenuCell = ComponentSubMenuCell;
            this.ComponentSubMenuCellOver = ComponentSubMenuCellOver;
        }

        public void Deserialize(XmlElement parentNode)
        {
            this.objectType = Type.GetType(parentNode.GetAttribute("ObjectType"));
            this.Component = parentNode.GetAttribute("Component");
            this.ComponentOver = parentNode.GetAttribute("ComponentOver");
            this.ComponentContentCell = parentNode.GetAttribute("ComponentContentCell");
            this.ComponentContentCellOver = parentNode.GetAttribute("ComponentContentCellOver");
            this.ComponentLeftRoundCornerCell = parentNode.GetAttribute("ComponentLeftRoundCornerCell");
            this.ComponentLeftRoundCornerCellOver = parentNode.GetAttribute("ComponentLeftRoundCornerCellOver");
            this.ComponentRightRoundCornerCell = parentNode.GetAttribute("ComponentRightRoundCornerCell");
            this.ComponentRightRoundCornerCellOver = parentNode.GetAttribute("ComponentRightRoundCornerCellOver");
            this.ComponentIconCell = parentNode.GetAttribute("ComponentIconCell");
            this.ComponentIconCellOver = parentNode.GetAttribute("ComponentIconCellOver");
            this.ComponentSubMenuCell = parentNode.GetAttribute("ComponentSubMenuCell");
            this.ComponentSubMenuCellOver = parentNode.GetAttribute("ComponentSubMenuCellOver");
        }

        public bool EqualsCSSClass(object obj)
        {
            if (!(obj is CSSClasses))
            {
                return base.Equals(obj);
            }
            CSSClasses classes = (CSSClasses) obj;
            return ((((((this.AppliesTo == classes.AppliesTo) && (this.Component == classes.Component)) && ((this.ComponentContentCell == classes.ComponentContentCell) && (this.ComponentContentCellOver == classes.ComponentContentCellOver))) && (((this.ComponentLeftRoundCornerCell == classes.ComponentLeftRoundCornerCell) && (this.ComponentLeftRoundCornerCellOver == classes.ComponentLeftRoundCornerCellOver)) && ((this.ComponentRightRoundCornerCell == classes.ComponentRightRoundCornerCell) && (this.ComponentRightRoundCornerCellOver == classes.ComponentRightRoundCornerCellOver)))) && (((this.ComponentIconCell == classes.ComponentIconCell) && (this.ComponentIconCellOver == classes.ComponentIconCellOver)) && ((this.ComponentSubMenuCell == classes.ComponentSubMenuCell) && (this.ComponentSubMenuCellOver == classes.ComponentSubMenuCellOver)))) && (this.ComponentOver == classes.ComponentOver));
        }

        public bool equalsToDefault()
        {
            return this.EqualsCSSClass(this.getDefaultForClass());
        }

        private CSSClasses getDefaultForClass()
        {
            return new CSSClasses(this.objectType);
        }

        public void Render(HtmlTextWriter writer)
        {
            if (((this.objectType.Name.ToLower() == "menuitem") || (this.objectType.Name.ToLower() == "menuseparator")) && this.equalsToDefault())
            {
                writer.Write("'" + this.objectType.Name + "':'default'");
            }
            else
            {
                writer.Write("'" + this.objectType.Name + "':{'c':'" + this.Component + "', 'c_o':'" + this.ComponentOver + "', 'c_c':'" + this.ComponentContentCell + "', 'c_c_o':'" + this.ComponentContentCellOver + "', 'c_lrc':'" + this.ComponentLeftRoundCornerCell + "', 'c_lrc_o':'" + this.ComponentLeftRoundCornerCellOver + "', 'c_rrc':'" + this.ComponentRightRoundCornerCell + "', 'c_rrc_o':'" + this.ComponentRightRoundCornerCellOver + "', 'c_i':'" + this.ComponentIconCell + "', 'c_i_o':'" + this.ComponentIconCellOver + "', 'c_m':'" + this.ComponentSubMenuCell + "', 'c_m_o':'" + this.ComponentSubMenuCellOver + "'}");
            }
        }

        public void Serialize(ref XmlElement parentNode)
        {
            parentNode.SetAttribute("ObjectType", this.objectType.ToString());
            parentNode.SetAttribute("Component", this.Component);
            parentNode.SetAttribute("ComponentOver", this.ComponentOver);
            parentNode.SetAttribute("ComponentContentCell", this.ComponentContentCell);
            parentNode.SetAttribute("ComponentContentCellOver", this.ComponentContentCellOver);
            parentNode.SetAttribute("ComponentLeftRoundCornerCell", this.ComponentLeftRoundCornerCell);
            parentNode.SetAttribute("ComponentLeftRoundCornerCellOver", this.ComponentLeftRoundCornerCellOver);
            parentNode.SetAttribute("ComponentRightRoundCornerCell", this.ComponentRightRoundCornerCell);
            parentNode.SetAttribute("ComponentRightRoundCornerCellOver", this.ComponentRightRoundCornerCellOver);
            parentNode.SetAttribute("ComponentIconCell", this.ComponentIconCell);
            parentNode.SetAttribute("ComponentIconCellOver", this.ComponentIconCellOver);
            parentNode.SetAttribute("ComponentSubMenuCell", this.ComponentSubMenuCell);
            parentNode.SetAttribute("ComponentSubMenuCellOver", this.ComponentSubMenuCellOver);
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string AppliesTo
        {
            get
            {
                if (this.objectType == null)
                {
                    return "undefined";
                }
                return this.objectType.Name;
            }
        }

        public string Component
        {
            get
            {
                return this._Component;
            }
            set
            {
                this._Component = value;
            }
        }

        public string ComponentContentCell
        {
            get
            {
                return this._ComponentContentCell;
            }
            set
            {
                this._ComponentContentCell = value;
            }
        }

        public string ComponentContentCellOver
        {
            get
            {
                return this._ComponentContentCellOver;
            }
            set
            {
                this._ComponentContentCellOver = value;
            }
        }

        public string ComponentIconCell
        {
            get
            {
                return this._ComponentIconCell;
            }
            set
            {
                this._ComponentIconCell = value;
            }
        }

        public string ComponentIconCellOver
        {
            get
            {
                return this._ComponentIconCellOver;
            }
            set
            {
                this._ComponentIconCellOver = value;
            }
        }

        public string ComponentLeftRoundCornerCell
        {
            get
            {
                return this._ComponentLeftRoundCornerCell;
            }
            set
            {
                this._ComponentLeftRoundCornerCell = value;
            }
        }

        public string ComponentLeftRoundCornerCellOver
        {
            get
            {
                return this._ComponentLeftRoundCornerCellOver;
            }
            set
            {
                this._ComponentLeftRoundCornerCellOver = value;
            }
        }

        public string ComponentOver
        {
            get
            {
                return this._ComponentOver;
            }
            set
            {
                this._ComponentOver = value;
            }
        }

        public string ComponentRightRoundCornerCell
        {
            get
            {
                return this._ComponentRightRoundCornerCell;
            }
            set
            {
                this._ComponentRightRoundCornerCell = value;
            }
        }

        public string ComponentRightRoundCornerCellOver
        {
            get
            {
                return this._ComponentRightRoundCornerCellOver;
            }
            set
            {
                this._ComponentRightRoundCornerCellOver = value;
            }
        }

        public string ComponentSubMenuCell
        {
            get
            {
                return this._ComponentSubMenuCell;
            }
            set
            {
                this._ComponentSubMenuCell = value;
            }
        }

        public string ComponentSubMenuCellOver
        {
            get
            {
                return this._ComponentSubMenuCellOver;
            }
            set
            {
                this._ComponentSubMenuCellOver = value;
            }
        }

        public string ObjectType
        {
            get
            {
                try
                {
                    return this.objectType.FullName;
                }
                catch
                {
                    return string.Empty;
                }
            }
            set
            {
                if (this.objectType == null)
                {
                    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
                    this.objectType = null;
                    for (int i = assemblies.Length - 1; i >= 0; i--)
                    {
                        if (assemblies[i].GetType(value) != null)
                        {
                            this.objectType = assemblies[i].GetType(value);
                            return;
                        }
                    }
                }
            }
        }
    }
}

